<?php
class Control_Data_getChannelData {

	function getChannelData() {
		$startDate 	= 	date('Y-m-d', time());
		$getGame = $game = MooForm::request('game');
		$date = MooForm::request('date');
		$type = MooForm::request('type');
		
		$gameLangConf = MooConfig::get('game_lang.zh_en');
		if($gameLangConf[$game]) {
			$game = $gameLangConf[$game];
		}
		
		$channel_lang = MooConfig::get("channel_lang.{$game}.en_zh");
		
		if($game && $date) {
			MooObj::get('Control_Data')->setPlatformData($game);
			$url = MooConfig::get('main.url.fetch_general');
			$getDataUrl = $url . "?game=" . $game . "&date=" . $date . "&type=2";
			
			$platformData = MooUtil::curl_send($getDataUrl);
			$platformDataArr  = MooJson::decode($platformData, true);
			
			// 转换百分数
		    foreach($platformDataArr as $key => $val) {
				foreach ($val as $k => $v) {
					if($v > 0 && $v < 1) {
					if (in_array($k, array("userRetention2", "deviceRetention2", "PayRate"))) {
							$platformDataArr[$key][$k] =  ($v*100)."%";
						}
					}
					// 渠道名称翻译
					if($k == "channel") {
						if($channel_lang[$v]) {
							$platformDataArr[$key][$k] = $channel_lang[$v];
						}
					}
				}
			}
			
			
			if($platformDataArr && $type == "daochu") {
				$excelData = array();
				$sDate = date("Ymd", strtotime($startDate));
				$name = $game . "_"  . $sDate;
			
				// 平台数据
				$dataDir = "channelData";
				/**
				 * 导出数据的表头对应字段
				 */
				$keyValues = array(
					'channel'=>'渠道',
					'newUser'=>'新增用户',
					'newDevice'=>'设备新增',
					'userLogin'=>'日活跃用户',
					'deviceLogin'=>'日活跃设备',
					'userRetention2'=>'用户次留',
					'deviceRetention2'=>'设备次留',
					'payDevice'=>'付费设备',
					'payMoney'=>'付费额',
					'PayRate'=>'付费率',
					'ARPU'=>'付费UP',
					'ARPPU'=>'人均UP',
				);
				MooObj::get('Data')->exportExcel($name, $dataDir, $platformDataArr, $keyValues);
			}
			
			
			
			MooView::set('nowGame', $getGame);
			MooView::set('startDate', $date);
			MooView::set('platformDataArr', $platformDataArr);
			MooView::render('platformData3');
		} else {
			MooView::set('startDate', $startDate);
			MooView::render('platformData3');
		}
	}
}